<?php
namespace app\lmsx\home;

use app\index\controller\Home;
use app\lmsx\model\User as UserModel;
use think\Db;

/**
 * 首页控制器
 * @author lan
 */


class Index extends Home
{
    private $appid='wxe40cfe48d66f2a27';
    private $secret='3ce33d83fc8c95c8f3de35a99a8d555a';

    /**
     * 微信授权code，获取openid
     * @return \think\response\Json
     */
    public function index()
    {
        if($this->request->post()){
            //code用户在微信登录的临时凭证
//            $weixin=request()->param();
            $weixin=request()->only(['code','NickName','HeadUrl','xcx_code','id_code']);
            //return json($weixin);
            $code=$weixin['code'];
            $data=[];
            if(empty($code)){
                header("HTTP/1.1 401 Not Found");   // 如果是404 改成404即可
                header("Status: 401 Not Found");
                header('Content-type: application/json; charset=utf-8');
                exit();
            }
            //登录凭证校验接口
            $url="https://api.weixin.qq.com/sns/jscode2session?appid=$this->appid&secret=$this->secret&js_code=$code&grant_type=authorization_code";

            //code获取用户openid和session_key
            $info = json_decode(get_curl($url), true);


            //dump($info);die;
            if(!isset($info['session_key'])) return mogo_error($info,401);
            //判断用户是否注册，如果没有则注册用户

            $row=Db::table('dp_lmsx_user')->where(['openid'=>$info['openid']])->find();

            if(empty($row)){//用户注册
                $data=[
                    'openid'=>$info['openid'],
                    'nickname'=>$weixin['NickName'],
                    'avatar'=>$weixin['HeadUrl'],
                ];
                if(isset($weixin['id_code'])){
                    $data['recommend_code']=$weixin['id_code'];
                }
                if(isset($weixin['xcx_code'])){
                    $data['xcx_code']=$weixin['xcx_code'];
                }
                //dump($data);die;
                UserModel::create($data);
            }else{//用户已经注册
                $data=[
                    'login_time'=>time(),
                    'id'=>$row['id'],
                ];
                UserModel::update($data);
                //dump($b);die;
            }
            session('openid',$info['openid']);
            return json($info['openid']);
        }else{
            return mogo_error('未登录',401);
        }
    }

    /**
     * 获取用户信息
     */
    public function getUserInfo(){
        $param=request()->param();
        if(empty($param['openid'])){
            return mogo_error('未授权登录',401);
        }
        $row=Db::table('dp_lmsx_user')
            ->where(['openid'=>$param['openid']])
            ->field('id,phone,role,code,city,name,pull_black')
            ->find();
        $code=Db::table('dp_lmsx_code')
            ->where(['user_id'=>$row['id']])->field('code')->find();
        if(empty($code)){
            $code['code']='';
        }
        $row['code']=$code['code'];
        $row['city_1']=['1'=>'衡阳','2'=>'株洲','3'=>'岳阳'];
        return mogo_json($row,1);
    }

//    public function test(){
//        $options=[
//            'app_id'=>$this->appid,
//            'secret'=>$this->secret,
//        ];
//        $app = new Application($options);
//        $userService = $app->user;
//        $user_m=$userService->get($info['openid']);
//        Cache('user_info',$user_m);
//    }



}